﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Company Search Page
// Created By:     Tingrong Shi (trshi@wicresoft.com)
// Created Date:   2011-2-14
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using ASBCC.Business.CIS;

namespace BEGDC.CC.Web.CIS2.Overseas.Search
{
    public partial class CompanySearch : System.Web.UI.Page
    {
        private static string Search = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.edsCompany.Where = Search;
            if (!this.IsPostBack)
                Bind();
        }

        private void Bind()
        {
            //get roles
            var roles = (this.User as CSPrincipal).Role2s;
            string allrole = "";
            if (roles != null && roles.Count() != 0)
            {
                foreach (var role in roles)
                {
                    allrole += role;
                    allrole += ",";
                }
                allrole = allrole.Substring(0, allrole.Length - 2);
            }
            
            //selectCountry
            selectCountry.DataSource = SearchHelper.GetCountryByRoleIDs(null,allrole);
            selectCountry.DataValueField = "CountryID";
            selectCountry.DataTextField = "Name";
            selectCountry.DataBind();
            selectCountry.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string companyName = this.txtCompanyName.Text;
            string Country = this.Request.Form["selectCountry"];
            string editer = this.txtEdit.Text;
            string customerIDs = SearchHelper.GetCompanyIDsBMainCIO(companyName, null,Country, editer);

            Search = "!it.Disabled and it.CompanyID in {" + customerIDs + "}";
            this.edsCompany.Where = Search;
            this.GVCompany.Visible = true;
            this.DVCompany.Visible = false;
            this.GVCompany.PageIndex = 0;

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.GVCompany.Visible = false;
            this.DVCompany.Visible = false;
            this.txtCompanyName.Text = "";
            this.txtEdit.Text = "";
            this.selectCountry.SelectedIndex = 0;
        }

        protected void GVCompany_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "open")
            {
                int index = int.Parse(e.CommandArgument as string) + GVCompany.PageIndex * GVCompany.PageSize;
                DVCompany.PageIndex = index;
                DVCompany.Visible = true;
            }
        }
    }
}
